package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author 加饭
 * @since 2021-10-26
 */
@Api(value = "讲师 前端控制器")
@RestController
@RequestMapping("/admin/edu/teacher")
@Slf4j
public class AdminTeacherController {
    @Autowired
    private TeacherService teacherService;

    @ApiOperation(value = "查询所有讲师数据")
    @GetMapping("list")
    public R getTeacher() {
        List<Teacher> teacherList = teacherService.list();
        return R.ok().data("items", teacherList);
    }

    @ApiOperation(value = "根据ID查询特定讲师数据")
    @GetMapping("getById/{id}")
    public R getById(@PathVariable("id") String id) {

//            int i = 1 / 0;

        Teacher teacher = teacherService.getById(id);
        return R.ok().data("item", teacher);
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("getPage/{pageNum}/{pageSize}")
    public R getPage(@ApiParam("页码") @PathVariable("pageNum") Integer pageNum, @ApiParam("页面条数") @PathVariable Integer pageSize) {
        Page<Teacher> page = teacherService.page(new Page<>(pageNum, pageSize));
        return R.ok().data("page", page);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("save")
    public R save(@RequestBody Teacher teacher) {
        boolean save = teacherService.save(teacher);
        return R.ok().data("flag", save);
    }

    @ApiOperation(value = "删除指定ID教师")
    @DeleteMapping("deleteById/{id}")
    public R deleteById(@ApiParam(value = "id",defaultValue = "1",required = true) @PathVariable Integer id) {
        teacherService.removeById(id);
        return R.ok();
    }

    @ApiOperation("修改指定教师信息")
    @PutMapping("updateById")
    public R updateById(@RequestBody Teacher teacher) {
        teacherService.updateById(teacher);
        return R.ok();
    }

}

